package com.mbc.hr.manager;

import java.sql.SQLException;
import java.util.List;

import com.mbc.common.data.DataList;
import com.mbc.common.exception.DataDuplicationException;
import com.mbc.common.exception.ForeignConflictException;
import com.mbc.common.manager.AbstractMgr;
import com.mbc.common.param.CriteriaParams;
import com.mbc.common.utils.KeyGenerator;
import com.mbc.hr.entities.Degree;

public class DegreeMgr extends AbstractMgr {
	
	public DegreeMgr(){
		
	}
	
	public DegreeMgr(String sessionId){
		this.sessionId = sessionId;
	}
	
	public void createDegree(Degree degree)throws Exception{
		openConnection();
		try {
			beginTransaction();
			long l_key  = KeyGenerator.generateKey(entityManager);
			
			//long l_key = Long.parseLong(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
			
			degree.setDegreeID(l_key);
			entityManager.persist(degree);
			commitTransaction();
		}
		catch (Exception e) {
			rollbackTransaction();
			if(DataDuplicationException.isUniqueKyeError(e))
				throw new DataDuplicationException(e.getMessage());
			else
				throw e;
		} finally{
			closeConnection();
		}
	}
	
	public void updateDegree(Degree degree)throws Exception{
		openConnection();
		try {
			beginTransaction();
			
			entityManager.merge(degree);
			commitTransaction();
		} catch (Exception e) {
			rollbackTransaction();
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public void deleteDegree(long degreeID)throws Exception{
		openConnection();
		try {
			beginTransaction();
			Degree l_degree = entityManager.find(Degree.class, degreeID);
			entityManager.remove(l_degree);
			commitTransaction();
		} catch (Exception e) {
			rollbackTransaction();
			if(e.getCause().getCause().getCause() instanceof SQLException){
				if(ForeignConflictException.FOREIGN_CONFLICT_ERROR_CODE == ((SQLException)e.getCause().getCause().getCause()).getErrorCode())
					throw new ForeignConflictException(e);
				else
					throw e;
			}
			else
				throw e;
		} finally {
			closeConnection();
		}
	}
	
	public Degree findDegree(long degreeID)throws Exception{
		openConnection();
		try {
			Degree l_degree = entityManager.find(Degree.class, degreeID);
			return l_degree;
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public List<Degree> findAllDegree()throws Exception{
		openConnection();
		try {
			return entityManager.createNamedQuery("Degree.findAll", Degree.class).getResultList();
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public DataList<Degree> findDegreebyCriteria(CriteriaParams cri) throws Exception{
		openConnection();
		try {
            String l_jpql = "select lt from Degree lt where lt.degreeName like '%" + cri.getKeyword() + "%'";
            
            List<Degree> l_entityList = entityManager.createQuery(l_jpql, Degree.class)
                    .setFirstResult(cri.getIndex())
                    .setMaxResults(cri.getSize())
                    .getResultList();
            
            DataList<Degree> l_dataList = new DataList<Degree>();//LeaveSetupDataList();
            l_dataList.setEntityList(l_entityList);
            
            if(cri.getIndex() == 0){
            	String l_countQuery = "select count(lt.degreeName) from Degree lt where lt.degreeName like '%" + cri.getKeyword() + "%'";
            	l_dataList.setTotalRecord(Long.parseLong(entityManager.createQuery(l_countQuery).getSingleResult().toString()));
            }
			return l_dataList;
		} catch (Exception e) {
			throw e;
		} finally{
			closeConnection();
		}	
	}
	

}
